import { _ as _export_sfc, x as useMainStore, y as storeToRefs, z as ref, al as useNavbarHeight, B as onLoad, R as onPullDownRefresh, W as stopPullDownRefresh, g as createElementBlock, b as createVNode, w as withCtx, n as normalizeStyle, H as unref, F as Fragment, j as resolveEasycom, i as index$g, G as __easycom_0, k as resolveDynamicComponent, o as openBlock, c as createBlock, e as createCommentVNode, P as ScrollView, h as renderList, p as index$q, t as createTextVNode, s as index$i, u as toDisplayString, X as formatDateTime, M as withModifiers, Q as index$x, at as prePage, ag as navigateBack } from './index-Cc4oxzDN.js';
import { _ as __easycom_4 } from './uv-empty.ob-CT4XT.js';
import { _ as __easycom_8 } from './modal._6MPzui9.js';
import { b as __easycom_5 } from './uv-toast.nS_Liirv.js';
import { c as couponMine } from './coupon.DpWQn5v_.js';
import './uv-loading-icon.DuNkB54M.js';

const _sfc_main = {
  __name: 'index',
  setup(__props) {

const main = useMainStore();
const { isLogin } = storeToRefs(main);
const title = ref('优惠券');

// 获取导航栏高度
const { navbarHeightCss } = useNavbarHeight();

	// 返回上一页
	const $onClickLeft = () => {
		navigateBack();
	};

const activeTabIndex = ref(0);
const detailModalVisible = ref(false);
const coupon = ref({});
const couponIndex = ref(0);
const coupons = ref([]);
const amount = ref(0);
const buttonLock = ref(false);
const coupon_id = ref(0);
const shop_id = ref(0);
const type = ref(0);
const uToast = ref();

onLoad((options) => {
	if (options.amount) {
		amount.value = options.amount;
	}
	if (options.coupon_id) {
		coupon_id.value = options.coupon_id;
	}
	if (options.shop_id) {
		shop_id.value = options.shop_id;
	}
	if (options.type) {
		type.value = options.type;
	}
	activeTabIndex.value = 0;
	getCoupons();
});

onPullDownRefresh(() => {
	getCoupons();
});

// 使用范围
const typeInfo = (type) => {
	if (type == 0) {
		return '外卖和自取';
	}
	if (type == 1) {
		return '自取';
	}
	if (type == 2) {
		return '外卖';
	}
};
const getCoupons = async() => {
	let data = await couponMine({shop_id: shop_id.value, type: type.value, page:1, pagesize:10000});
	stopPullDownRefresh();
	if (data) {
		coupons.value = data;
	}
};
const openDetailModal = (mycoupon, index) => {
	couponIndex.value = index;
	coupon.value = mycoupon;
	detailModalVisible.value = true;
};
// 使用优惠券
const useCouponWith = (mycoupon) => {
	coupon.value = mycoupon;
	useCoupon();
};
// 取消优惠券
const cancelCoupon = () => {
	coupon.value = {};
	coupon_id.value = 0;
	prePage().coupon = {};
};
const closeDetailModal = () => {
	detailModalVisible.value = false;
	coupon.value = {};
};
// 使用优惠及

const useCoupon = () => {
	if (buttonLock.value == true) {
		return;
	}
	buttonLock.value = true;
	//console.log('coupon:',coupon.value);
	if (parseFloat(coupon.value.least) > parseFloat(amount.value)) {
		//console.log('pages3:');
		uToast.value.show({
			message: '订单金额满'+coupon.value.least+'才能使用',
			type: 'error'
		});
		buttonLock.value = false;
	} else {
	    main.SET_COUPON(coupon);
		console.log('main.myconpon:',main.mycoupon);
		//prePage().coupon = coupon.value;
		//prePage().coupons = 1; // 哨兵
		
		navigateBack({
			
		});
		
	}
	
};




return (_ctx, _cache) => {
  const _component_uv_navbar = resolveEasycom(resolveDynamicComponent("uv-navbar"), __easycom_0);
  const _component_uv_empty = resolveEasycom(resolveDynamicComponent("uv-empty"), __easycom_4);
  const _component_v_uni_image = index$q;
  const _component_v_uni_text = index$i;
  const _component_v_uni_view = index$g;
  const _component_v_uni_scroll_view = ScrollView;
  const _component_v_uni_button = index$x;
  const _component_modal = resolveEasycom(resolveDynamicComponent("modal"), __easycom_8);
  const _component_uv_toast = resolveEasycom(resolveDynamicComponent("uv-toast"), __easycom_5);

  return (openBlock(), createElementBlock(Fragment, null, [
    createVNode(_component_uv_navbar, {
      fixed: true,
      title: title.value,
      "left-arrow": "",
      onLeftClick: $onClickLeft
    }, null, 8, ["title"]),
    createVNode(_component_v_uni_view, {
      class: "container position-relative w-100 h-100 overflow-hidden",
      style: normalizeStyle(`padding-top: ${unref(navbarHeightCss)};`)
    }, {
      default: withCtx(() => [
        (coupons.value.length == 0)
          ? (openBlock(), createBlock(_component_uv_empty, {
              key: 0,
              mode: "coupon"
            }))
          : createCommentVNode("", true),
        createVNode(_component_v_uni_scroll_view, {
          "scroll-y": "",
          class: "coupon-list"
        }, {
          default: withCtx(() => [
            createVNode(_component_v_uni_view, { class: "wrapper" }, {
              default: withCtx(() => [
                (openBlock(true), createElementBlock(Fragment, null, renderList(coupons.value, (item, index) => {
                  return (openBlock(), createBlock(_component_v_uni_view, {
                    class: "coupon",
                    key: index,
                    onClick: $event => (openDetailModal(item, index))
                  }, {
                    default: withCtx(() => [
                      createVNode(_component_v_uni_view, { class: "taobao" }, {
                        default: withCtx(() => [
                          createVNode(_component_v_uni_view, {
                            class: "ticket",
                            style: normalizeStyle({border: item.id == coupon_id.value ? '1rpx solid red':''})
                          }, {
                            default: withCtx(() => [
                              createVNode(_component_v_uni_view, { class: "left" }, {
                                default: withCtx(() => [
                                  createVNode(_component_v_uni_image, {
                                    class: "picture",
                                    src: item.image,
                                    mode: "aspectFill"
                                  }, null, 8, ["src"]),
                                  createVNode(_component_v_uni_view, { class: "introduce" }, {
                                    default: withCtx(() => [
                                      createVNode(_component_v_uni_view, { class: "top" }, {
                                        default: withCtx(() => [
                                          createTextVNode(" ￥ "),
                                          createVNode(_component_v_uni_text, { class: "big" }, {
                                            default: withCtx(() => [
                                              createTextVNode(toDisplayString(item.value), 1)
                                            ]),
                                            _: 2
                                          }, 1024),
                                          createVNode(_component_v_uni_view, null, {
                                            default: withCtx(() => [
                                              createTextVNode("满" + toDisplayString(item.least) + "减" + toDisplayString(item.value), 1)
                                            ]),
                                            _: 2
                                          }, 1024)
                                        ]),
                                        _: 2
                                      }, 1024),
                                      createVNode(_component_v_uni_view, { class: "type" }, {
                                        default: withCtx(() => [
                                          createTextVNode(toDisplayString(item.title), 1)
                                        ]),
                                        _: 2
                                      }, 1024),
                                      createVNode(_component_v_uni_view, { class: "date u-line-1" }, {
                                        default: withCtx(() => [
                                          createTextVNode(toDisplayString(unref(formatDateTime)(item.startTime, 'yyyy-MM-dd')) + "-" + toDisplayString(unref(formatDateTime)(item.endTime, 'yyyy-MM-dd')), 1)
                                        ]),
                                        _: 2
                                      }, 1024)
                                    ]),
                                    _: 2
                                  }, 1024)
                                ]),
                                _: 2
                              }, 1024),
                              (activeTabIndex.value == 0)
                                ? (openBlock(), createBlock(_component_v_uni_view, {
                                    key: 0,
                                    class: "right",
                                    onClick: _cache[0] || (_cache[0] = withModifiers(() => {}, ["stop"]))
                                  }, {
                                    default: withCtx(() => [
                                      (item.id != coupon_id.value)
                                        ? (openBlock(), createBlock(_component_v_uni_view, {
                                            key: 0,
                                            class: "use immediate-use",
                                            round: true,
                                            onClick: $event => (useCouponWith(item))
                                          }, {
                                            default: withCtx(() => [
                                              createTextVNode("立即使用")
                                            ]),
                                            _: 2
                                          }, 1032, ["onClick"]))
                                        : (openBlock(), createBlock(_component_v_uni_view, {
                                            key: 1,
                                            class: "use immediate-use",
                                            round: true,
                                            onClick: $event => (cancelCoupon(item))
                                          }, {
                                            default: withCtx(() => [
                                              createTextVNode("取消使用")
                                            ]),
                                            _: 2
                                          }, 1032, ["onClick"]))
                                    ]),
                                    _: 2
                                  }, 1024))
                                : createCommentVNode("", true)
                            ]),
                            _: 2
                          }, 1032, ["style"])
                        ]),
                        _: 2
                      }, 1024)
                    ]),
                    _: 2
                  }, 1032, ["onClick"]))
                }), 128))
              ]),
              _: 1
            })
          ]),
          _: 1
        }),
        createVNode(_component_modal, {
          custom: "",
          show: detailModalVisible.value,
          onCancel: closeDetailModal,
          width: "90%"
        }, {
          default: withCtx(() => [
            createVNode(_component_v_uni_view, { class: "modal-content" }, {
              default: withCtx(() => [
                createVNode(_component_v_uni_view, { class: "d-flex font-size-extra-lg text-color-base just-content-center mb-20" }, {
                  default: withCtx(() => [
                    createTextVNode(toDisplayString(coupon.value.title), 1)
                  ]),
                  _: 1
                }),
                createVNode(_component_v_uni_view, { class: "d-flex font-size-sm text-color-base mb-20" }, {
                  default: withCtx(() => [
                    createTextVNode("有效期：" + toDisplayString(unref(formatDateTime)(coupon.value.startTime, 'yyyy-MM-dd')) + "至" + toDisplayString(unref(formatDateTime)(coupon.value.endTime, 'yyyy-MM-dd')), 1)
                  ]),
                  _: 1
                }),
                createVNode(_component_v_uni_view, { class: "d-flex font-size-sm text-color-base mb-20" }, {
                  default: withCtx(() => [
                    createTextVNode("领取时间：" + toDisplayString(coupon.value.createtime_text), 1)
                  ]),
                  _: 1
                }),
                createVNode(_component_v_uni_view, { class: "d-flex font-size-sm text-color-base mb-20" }, {
                  default: withCtx(() => [
                    createTextVNode("卷价值：满" + toDisplayString(coupon.value.least) + "减" + toDisplayString(coupon.value.value), 1)
                  ]),
                  _: 1
                }),
                createVNode(_component_v_uni_view, { class: "d-flex font-size-sm text-color-base mb-20" }, {
                  default: withCtx(() => [
                    createTextVNode("适用范围：" + toDisplayString(typeInfo(coupon.value.type)), 1)
                  ]),
                  _: 1
                }),
                createVNode(_component_v_uni_view, { class: "d-flex font-size-sm text-color-base mb-20" }, {
                  default: withCtx(() => [
                    createTextVNode("适用店铺：" + toDisplayString(coupon.value.shopName), 1)
                  ]),
                  _: 1
                }),
                (activeTabIndex.value == 0)
                  ? (openBlock(), createBlock(_component_v_uni_view, {
                      key: 0,
                      class: "d-flex align-items-center just-content-center"
                    }, {
                      default: withCtx(() => [
                        createVNode(_component_v_uni_button, {
                          type: "primary",
                          onClick: useCoupon,
                          class: "use-coupon-btn"
                        }, {
                          default: withCtx(() => [
                            createTextVNode("立即使用")
                          ]),
                          _: 1
                        })
                      ]),
                      _: 1
                    }))
                  : createCommentVNode("", true)
              ]),
              _: 1
            })
          ]),
          _: 1
        }, 8, ["show"]),
        createVNode(_component_uv_toast, {
          ref_key: "uToast",
          ref: uToast
        }, null, 512)
      ]),
      _: 1
    }, 8, ["style"])
  ], 64))
}
}

};
const index = /*#__PURE__*/_export_sfc(_sfc_main, [['__scopeId',"data-v-5dbb19bd"]]);

export { index as default };
